package com.cocola.service;

import com.cocola.mapper.UserMapper;
import com.cocola.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> test() {
        System.out.println(("----- selectAll method test ------"));
        List<User> users = userMapper.selectList(null);
        return users;
    }

    /**
     *查找条件匹配 userName 的全部
     */
    public List<User> findUser(String userName) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("user_name", userName);

        return userMapper.selectByMap(map);
    }

    /**
     *注册
     */
    public void register(String userName, String password, String passswordConfirm, String securityQuestion, String securityAnswer, String phone, String securityCode,
                         String email, String userIdcard, String idType) {

        System.out.println("=============>insert user");

        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        user.setPasswordConfirm(passswordConfirm);
        user.setSecurityQuestion(securityQuestion);
        user.setSecurityAnswer(securityAnswer);
        user.setPhone(phone);
        user.setSecurityCode(securityCode);
        user.setEmail(email);
        user.setUserIdcard(userIdcard);
        user.setIdType(idType);
        int insert = userMapper.insert(user);

        System.out.println(insert);
    }

    /**
     *登陆
     */
    public boolean login(String userName, String password) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("user_name", userName);
        List<User> user = userMapper.selectByMap(map);

        if(user.get(0).getPassword().equals(password)) {
            return true;
        }else {
            return false;
        }
    }



}
